package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcSaleService;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 销售出库
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/sale_out")
@RestController
public class XqcSaleOutController {

    /** 销售服务 */
    @Autowired
    private IXqcSaleService saleService;

    /**
     * 保存销售出库单(到审核阶段)
     */
    @PostMapping("/save_user_sale_out")
    @VtuzxApi
    public Object saveUserSaleOut(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return saleService.saveUserSaleOut( _userId, name, param);

    }

    /**
     * 添加用户临时销售出库物料箱号
     */
    @PostMapping("/add_user_saleout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
    } )
    public Object addUserSaleOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        int count = VtuzxUtil.getInt(param, "count");
        return saleService.addUserSaleOutMaterialBox(_userId, name, stockNo, boxNo, count);
    }

    /**
     * 添加用户临时其他出库物料箱号
     */
    @PostMapping("/add_user_saleout_material_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号列表", notNull = true),
    } )
    public Object addUserSaleOutMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        return saleService.addUserSaleOutMaterialBoxList(_userId, name, stockNo, boxList);
    }

    /**
     * 删除用户临时销售出库物料箱号
     */
    @PostMapping("/delete_user_saleout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserSaleOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return saleService.deleteUserSaleOutMaterialBox(_userId, id);
    }

    /**
     * 删除用户临时销售出库物料
     */
    @PostMapping("/delete_user_saleout_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserSaleOutMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        saleService.deleteUserSaleOutMaterial(_userId, id);
        return null;
    }

    /**
     * 删除用户临时销售出库单(清空)
     */
    @PostMapping("/delete_user_saleout")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object deleteUserSaleOut(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        saleService.deleteUserSaleOut(_userId, stockNo);
        return null;
    }

    /**
     * 编辑用户临时销售出库物料箱号
     */
    @PostMapping("/update_user_saleout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object updateUserSaleOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return saleService.updateUserSaleOutMaterialBox(_userId, stockNo, materialNo, boxNo, count);
    }

    /**
     * 获取用户临时销售出库物料列表
     */
    @PostMapping("/get_user_saleout_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getUserSaleOutMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return saleService.getUserSaleOutMaterial(_userId, stockNo);
    }

    /**
     * 获取用户临时销售出库物料箱号列表
     */
    @PostMapping("/get_user_saleout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "_detailId", label = "detailId", notNull = true),
    } )
    public Object getUserSaleOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return saleService.getUserSaleOutMaterialBox(_userId, _detailId);
    }

    /**
     * 获取销售出库单
     */
    @PostMapping("/get_saleout_bill")
    @VtuzxApi
    public Object getSaleOutBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return saleService.getSaleOutBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取销售出库单分录列表
     */
    @PostMapping("/get_saleout_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getSaleOutBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return saleService.getSaleOutBillDetail(_userId, stockNo, billNo);
    }

    /**
     * 获取销售出库单分录的箱号详细
     */
    @PostMapping("/get_saleout_bill_detail_box")
    @VtuzxApi
    public Object getSaleOutBillDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return saleService.getSaleOutBillDetailBox(_userId, _detailId);
    }

    /**
     * 文件上传(门店调拨)
     */
    @PostMapping("/upload_store_bill")
    @VtuzxApi
    public Object uploadStoreBill(@VtuzxControlParam VtuzxToken token,  @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        saleService.uploadStoreBill(token, param, vFileList);
        return null;
    }


    /**
     * 文件上传(门店销售)
     */
    @PostMapping("/upload_store_sale_bill")
    @VtuzxApi
    public Object uploadStoreSaleBill(@VtuzxControlParam VtuzxToken token,  @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        saleService.uploadStoreSaleBill(token, param, vFileList);
        return null;
    }


    /**
     * 获取门店调拨订单
     */
    @PostMapping("/get_sale_store_bill")
    @VtuzxApi
    public Object getSaleStoreBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return saleService.getSaleStoreBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取门店调拨订单分录列表
     */
    @PostMapping("/get_sale_store_bill_detail")
    @VtuzxApi
    public Object getSaleStoreBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return saleService.getSaleStoreBillDetail(_userId, billNo);
    }

    /**
     * 门店销售单批量出库
     */
    @PostMapping("/store_sale_bill_out_list")
    @VtuzxApi
    public Object storeSaleBillOutList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        saleService.storeSaleBillOutList(_userId, name, billNoList);
        return null;
    }

    /**
     * 获取门店销售订单
     */
    @PostMapping("/get_sale_store_sale_bill")
    @VtuzxApi
    public Object getSaleStoreSaleBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return saleService.getSaleStoreSaleBill(_userId, param, pageIndex, pageSize);
    }


    /**
     * 获取门店销售订单分录列表
     */
    @PostMapping("/get_sale_store_sale_bill_detail")
    @VtuzxApi
    public Object getSaleStoreSaleBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return saleService.getSaleStoreSaleBillDetail(_userId, billNo);
    }


    /**
     * 删除箱子扫码追溯码
     */
    @PostMapping("/delete_bind_trace_back")
    @VtuzxApi
    public Object deleteBindTraceBack(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        String traceBack = VtuzxUtil.getString(param, "traceBack");
        saleService.deleteBindTraceBack(_userId, boxNo, traceBack);
        return null;
    }


    /**
     * 文件上传(经销商)
     */
    @PostMapping("/upload_distributor_bill")
    @VtuzxApi
    public Object uploadDistributorBill(@VtuzxControlParam VtuzxToken token,  @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        saleService.uploadDistributorBill(token, param, vFileList);
        return null;
    }

    /**
     * 获取销售出库单
     */
    @PostMapping("/get_jky_saleout")
    @VtuzxApi
    public Object getjkySaleOut(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return saleService.getjkySaleOut(_userId, param, pageIndex, pageSize);
    }
}
